<?php
	defined("AT_EXE") or die ("CORE VARIABLE IS NOT DEFINED.");
	class AdminController extends Controller {
		public function __construct(){
			$this->calledClass(get_class($this));
		}
		public function index(){
			$this->set(array("current_menu"=>"index"));
			$employer = $this->loadModel("Employer");
			$candidate = $this->loadModel("Candidate");
			$order = $this->loadModel("Order");
			$job = $this->loadModel("Job");
			$resume = $this->loadModel("Resume");
			$this->set("empNonView",$candidate->countNonView("admin_view","Employers"));
			$this->set("canNonView",$candidate->countNonView("admin_view","Candidates"));
			$this->set("packNonView",$candidate->countNonView("status","Orders"));
			$this->set("subNonView",$candidate->countNonView("status","Orders"));
			$this->set("jobNonView",$candidate->countNonView("admin_view","Jobs"));
			$this->set("remNonView",$candidate->countNonView("cv_status_view","Resumes"));
			$this->render("index");
		}
		public function mainpageMMM(){
			$employer = $this->loadModel("Employer");
			$category = $this->loadModel("Categorie");
			$candidate = $this->loadModel("Candidate");
			$job = $this->loadModel("Job");
			$cv = $this->loadModel("Resume");
			$subscription = $this->loadModel("Subscription");
			$currency = $this->loadModel("Currency");
			
			$subscriptions = $subscription->getList(1,20,"",array("title","price"),array());
			$this->set(array("subscriptions"=>$subscriptions));
			$currencies = $currency->getCurrenciesAsArray();
			
			$this->set(array("currencies"=>$currencies));
			
			$resumes = $cv->getList(1,20,"",array("rem_name","rem_email","rem_tel","expire","rem_city","rem_province","rem_country"),array());
			$this->set(array("resumes"=>$resumes));
			
			$candidates = $candidate->getList(1,20,"",array("firstname","lastname","username","email"),array());
			$this->set(array("candidates"=>$candidates));
			
			$jobs = $job->getList(1,20,"",array("title","company_name"),array());
			$this->set(array("jobs"=>$jobs));
			$categories = $category->getCategoriesAsArray();
			$this->set(array("categoryrecords"=>$categories));
			
				
			$employers = $employer->getList(1,20,"",array("firstname","lastname","username","email","bussiness"),array());
			$this->set(array("employers"=>$employers));
			
			$this->set(array("current_menu"=>"main"));
			$this->render("mainpage");
		}
		public function mainpage(){
			$employer = $this->loadModel("Employer");
			$category = $this->loadModel("Categorie");
			$candidate = $this->loadModel("Candidate");
			$job = $this->loadModel("Job");
			$cv = $this->loadModel("Resume");
			$subscription = $this->loadModel("Subscription");
			$currency = $this->loadModel("Currency");
			
			$subscriptions = $subscription->getList(1,20,"",array("title","price"),array());
			$this->set(array("subscriptions"=>$subscriptions));
			$currencies = $currency->getCurrenciesAsArray();
			$this->set(array("currencies"=>$currencies));
			
			$start_dateJ=urldecode(trim ( $this->get("Jfrom") ) );
			$end_dateJ=urldecode(trim ( $this->get("Jto") ) );
			$start_dateJ=date("Y-m-d",strtotime($start_dateJ));
			$end_dateJ=date("Y-m-d",strtotime($end_dateJ));
			
			$start_dateC=urldecode(trim ( $this->get("Cfrom") ) );
			$end_dateC=urldecode(trim ( $this->get("Cto") ) );
			$start_dateC=date("Y-m-d",strtotime($start_dateC));
			$end_dateC=date("Y-m-d",strtotime($end_dateC));
			
			$start_dateR=urldecode(trim ( $this->get("Rfrom") ) );
			$end_dateR=urldecode(trim ( $this->get("Rto") ) );
			$start_dateR=date("Y-m-d",strtotime($start_dateR));
			$end_dateR=date("Y-m-d",strtotime($end_dateR));
			
			$start_date=urldecode(trim ( $this->get("dptfrom") ) );
			$end_date=urldecode(trim ( $this->get("dptto") ) );
			$start_date=date("Y-m-d",strtotime($start_date));
			$end_date=date("Y-m-d",strtotime($end_date));
			
			if($this->get("Jfrom")){
				$this->set(array("listjob"=>$employer->getJobPReport($this->get("page"),15,$start_dateJ,$end_dateJ)));
				$this->set(array("joblist"=>"joblist"));
				$this->set(array("start_dateJ"=>$start_dateJ));
				$this->set(array("end_dateJ"=>$end_dateJ));
			}else{
			$this->set(array("JobAday"=>$job->getJobdisplay($this->get("page"),15,$start_date,$end_date)));
			$this->set(array("JobCountAday"=>"JobCountAday"));
			unset($_SESSION['countcv']);
			}
			
			if($this->get("Rfrom")){
				$this->set(array("listresume"=>$employer->getCVReport($this->get("page"),15,$start_dateR,$end_dateR)));
				$this->set(array("listrematt"=>$employer->getCVReport1($this->get("page"),15,$start_dateR,$end_dateR)));
				$this->set(array("start_dateR"=>$start_dateR));
				$this->set(array("end_dateR"=>$end_dateR));
			}else{
			$this->set(array("ResumeAday"=>$cv->getResumedisplay($this->get("page"),15,$start_date,$end_date)));
			$this->set(array("ResumeCountAday"=>"ResumeCountAday"));
			unset($_SESSION['countcv']);
			}
			
			if($this->get("Cfrom")){
				$this->set(array("listcandidate"=>$employer->getJobSeekerReport($this->get("page"),15,$start_dateC,$end_dateC)));
				$this->set(array("start_dateC"=>$start_dateC));
				$this->set(array("end_dateC"=>$end_dateC));
			}else{
			$this->set(array("CandidateAday"=>$candidate->getCandidattedisplay($this->get("page"),15,$start_date,$end_date)));
			$this->set(array("canCountAday"=>"canCountAday"));
			unset($_SESSION['countcv']);
			}
			
			if($this->get("dptfrom")){
				$this->set(array("list"=>$employer->getEmployerReport($this->get("page"),15,$start_date,$end_date)));
				$this->set(array("start_date"=>$start_date));
				$this->set(array("end_date"=>$end_date));
			}else{
			$this->set(array("empTodaydisplay"=>$employer->getEmployerReportdisplay($this->get("page"),15,$start_date,$end_date)));
			$this->set(array("empcounttoday"=>"empcounttoday"));
			unset($_SESSION['countcv']);
			}
			
			$empToday=$employer->getEmployerReportToday() ;
			$candToday=$employer->getJobSeekerReportToday() ;
			$cvAttToday=$employer->getResumeAttachmentReportToday() ;
			$cvNoAttToday=$employer->getResumeNoAttachmentReportToday() ;
			$jobStandardToday=$employer->getJobStandardReportToday() ;
			$jobPremiumToday=$employer->getJobPremiumReportToday() ;
			$totalAllpost = $employer->getAlljobToday();
				
			$this->set(array("empToday"=>$empToday));
			$this->set(array("candToday"=>$candToday));
			$this->set(array("cvAttToday"=>$cvAttToday));
			$this->set(array("cvNoAttToday"=>$cvNoAttToday));
			$this->set(array("jobStandardToday"=>$jobStandardToday));
			$this->set(array("jobPremiumToday"=>$jobPremiumToday));
			$this->set(array("totalJobToday"=>$totalAllpost));
			//set job information
			$this->set(array("standard_internal"=>$job->get_standard_internal()));
			$this->set(array("standard_external"=>$job->get_standard_external()));
			$this->set(array("premium_internal"=>$job->get_premium_internal()));
			$this->set(array("premium_external"=>$job->get_premium_external()));
			
			$this->set(array("current_menu"=>"main"));
			$this->render("mainpage");
		}
		
		public function employer(){
			$employer = $this->loadModel("Employer");
			$category = $this->loadModel("Categorie");
			
			$categories = $category->getCategoriesAsArray();
			$this->set(array("categories"=>$categories));
			
			$employers = $employer->getList(1,20,"",array("firstname","lastname","username","email","bussiness"),array("id"),"DESC");
			$this->set(array("records"=>$employers));
			$this->set(array("current_menu"=>"company"));
			$this->render("employer");
		}
		public function candidate(){
			$condidate = $this->loadModel("Candidate");
			$category = $this->loadModel("Categorie");
				
			$categories = $category->getCategoriesAsArray();
			$this->set(array("categories"=>$categories));
			
			//$records = $condidate->getList($page=1,$perpage=20,$search="",$searchFields=array(),$sortFields=array(),$sortCriteria="");
			$records = $condidate->getList(1,20,"",array("firstname","lastname","username","email"),array("id"),"DESC");
			$this->set(array("records"=>$records));
			$this->set(array("current_menu"=>"seeker"));
			$this->render("jobseeker");
		}
		public function job(){
			$job = $this->loadModel("Job");
			$employer = $this->loadModel("Employer");
			$category = $this->loadModel("Categorie");
			//$records = $condidate->getList($page=1,$perpage=20,$search="",$searchFields=array(),$sortFields=array(),$sortCriteria="");
			//$records = $job->getList(1,20,"",array("title","posted_by"),array("id"),"DESC");
			$records = $job->getList(
					$this->get("page"),
					$this->get("perpage"),
					$this->get("search"),
					array("title","qualification","deadline"),
					array("id"),
					"DESC",
					array("location_category="=>$this->get("locations"),
							"company_name="=>$this->get("company"),
							"post_mode="=>$this->get("type")
					)
			);
			$categories = $category->getCategoriesAsArray();
			$employers = $employer->getEmployersAsArray();
			$this->set(array("records"=>$records));
			$this->set(array("categories"=>$categories));
			$this->set(array("current_menu"=>"job"));
			$this->set(array("com"=>$this->get("company"),"locat"=>$this->get("locations"),"p"=>$this->get("type")));
			$this->set(array("companies"=>$category->getCompanies()));
			$this->set(array("location"=>$category->getCategories(1)));
			$this->set(array("post"=>$category->getCategories(222)));
			$this->render("job");
		}
		public function cv(){
			$cv = $this->loadModel("Resume");
			$this->set(array("Location"=>$cv->SelectQu(1)));
			$this->set(array("Education"=>$cv->SelectQu(138)));
			$this->set(array("Experience"=>$cv->SelectQu(133)));
			$this->set(array("National"=>$cv->SelectQu(34)));
			$this->set(array("Province"=>$cv->SelectQu(99)));
			//$records = $condidate->getList($page=1,$perpage=20,$search="",$searchFields=array(),$sortFields=array(),$sortCriteria="");
			
			$category = $this->loadModel("Categorie");
			$categories = $category->getCategoriesAsArray();
			
			//$records = $cv->getList(1,20,"",array("fistname","lastname","fistname","rem_name","rem_email","rem_tel","expire","rem_city","rem_province","rem_country"),array());
			$records = $cv->getList(
					$this->get("page"),
					$this->get("perpage"),
					$this->get("search"),
					array("rem_name","rem_email","rem_salary","rem_position","firstname","lastname"),
					array("id"),
					"DESC",
					array("rem_location="=>$this->get("locations"),
							"rem_educate="=>$this->get("education"),
							"rem_province="=>$this->get("province"),
							"rem_experience_y="=>$this->get("experience"),
							"rem_city="=>$this->get("city"),
							"rem_country="=>$this->get("country")
					)
			);
			$this->set(array("records"=>$records));
			$this->set(array("current_menu"=>"cv"));
			$this->set(array("l"=>$this->get("locations"),
					"e"=>$this->get("education"),
					"p"=>$this->get("province"),
					"ex"=>$this->get("experience"),
					"ct"=>$this->get("city"),
					"c"=>$this->get("country")));
			$this->render("resume");
		}
		
		public function posting(){
			$post = $this->loadModel("Post");
			//$records = $condidate->getList($page=1,$perpage=20,$search="",$searchFields=array(),$sortFields=array(),$sortCriteria="");
			$records = $post->getList(1,20,"",array("emp_id","post_date","created"),array());
			$this->set(array("records"=>$records));
			$this->set(array("current_menu"=>"post"));
			$this->render("posting");
		}
		public function advertisement(){
			$advert = $this->loadModel("Advert");
			//echo $this->get("p");exit();
			$position = $this->get("p");
			$this->set(array("position"=>$position));
			$this->set(array("positions"=>array(
					0=>"Select positions",
					1=>"Top Left",
					2=>"Top Center",
					3=>"Top Right",
					4=>"Middle Left",
					5=>"Middle Center",
					6=>"Middle Right",
					7=>"Bottom",
			))); 
			$records = $advert->getList(1,20,"",array("location","url","owner","price"),array(),"",array("position="=>$position));
			$this->set(array("records"=>$records));
			$this->set(array("current_menu"=>"ads"));
			$this->render("advertisement");
		}
		public function subscription(){
			$subscription = $this->loadModel("Subscription");
			$currency = $this->loadModel("Currency");
			//$records = $condidate->getList($page=1,$perpage=20,$search="",$searchFields=array(),$sortFields=array(),$sortCriteria="");
			$records = $subscription->getList(1,20,"",array("title","price"),array());
			$this->set(array("records"=>$records));
			$currencies = $currency->getCurrenciesAsArray();
			$this->set(array("currencies"=>$currencies));
			$this->set(array("current_menu"=>"subscribe"));
			$this->render("subscription");
		}
		public function credit(){
			$package = $this->loadModel("Package");
			$currency = $this->loadModel("Currency");
			$query = $package->queryJoinTable();
			$this->set(array("getlist"=>$package->queryList()));
			$this->set(array("records"=>$package->DisplayAdmin($query,$this->get("page"),$this->get("perpage")?$this->get("perpage"):20,array("title"=>$this->get("title"),"price"=>$this->get("price"),"point"=>$this->get("point"),"valid_day"=>$this->get("validday")),array("id"),"ASC","AND")));
			$currencies = $currency->getCurrenciesAsArray();
			$this->set(array("currencies"=>$currencies));
			$this->set(array("current_menu"=>"credit"));
			$this->render("credit");
		}
		public function orderpackage(){
			$order = $this->loadModel("Order");
			$this->set(array("queryorder"=>$order->QueryPrice()));
			$this->set(array("queryPrices"=>$order->queryPriceFromPackage()));
			$query = $order->QueryOrder("Package");
			$this->set(array("records"=>$order->AdminOrder($query,$this->get("page"),$this->get("perpage")?$this->get("perpage"):20,$this->get("search"),array("company_name"),array("buy_date"),"ASC")));
			$this->set(array("getOrderByType"=>$order->GetOrderByType("Package")));
			$this->set("urls",$this->get("value_radio")?$this->get("value_radio"):"");
			$this->set("item",$this->get("search")?$this->get("search"):"");
			$this->set("perPage",$this->get("perpage")?$this->get("perpage"):20);
			$this->set(array("current_menu"=>"order"));
			$this->render("orderpackage");
		}
		public function ordersubscription(){
			$order = $this->loadModel("Order");
			$order = $this->loadModel("Order");
			$this->set(array("queryorder"=>$order->QueryPrice()));
			$this->set(array("queryPrices"=>$order->queryPriceFromPackage()));
			$query = $order->QueryOrder("Subscription");
			$this->set(array("records"=>$order->AdminOrder($query,$this->get("page"),$this->get("perpage")?$this->get("perpage"):20,$this->get("search"),array("company_name"),array("buy_date"),"ASC")));
			$this->set(array("getOrderByType"=>$order->GetOrderByType("Package")));
			$this->set("urls",$this->get("value_radio")?$this->get("value_radio"):"");
			$this->set("item",$this->get("search")?$this->get("search"):"");
			$this->set("perPage",$this->get("perpage")?$this->get("perpage"):20);
			$this->set(array("current_menu"=>"ordersubscription"));
			$this->render("ordersubscription");
		}
		public function mails(){
			$group = $this->loadModel("Group");
			$email_group = $this->loadModel("EmailGroup");
			$this->set(array("list"=>$email_group->getList($this->get("page"),20)));
			$this->set(array("allgroup"=>$group->getList()));
			$this->set(array("current_menu"=>"mail"));
			$this->render("mail");
		}
		public function addNewGroup(){
			$group_name= $this->get("group");
			//$email_add = $this->get("email");
			$group = $this->loadModel("Group");
			$group->save(array(
					"name"=>$group_name,
					"created"=>Date('Y-m-d')
					));
			
				$this->redirect(array("admin"=>"mail"));
			
			//$this->set(array("current_menu"=>"mail"));
			//$this->render("mail");
		}
		public function letter(){
			$group = $this->loadModel("Group");
			$this->set(array("allgroup"=>$group->getList()));
			$this->set(array("current_menu"=>"letter"));
			$this->render("letter");
		}
		public function report(){
			$this->set(array("current_menu"=>"report"));
			$employer=$this->loadModel("Employer");
			$candidate=$this->loadModel("Candidate");
			$start_date=urldecode(trim ( $this->get("dptFrom") ) );
			$end_date=urldecode(trim ( $this->get("dptTo") ) );
			$start_date=date("Y-m-d",strtotime($start_date));
			$end_date=date("Y-m-d",strtotime($end_date));
			if( $this->get("chkEmp")==1){
				$this->set(array("list"=>$employer->getEmployerReport($this->get("page"),15,$start_date,$end_date)));
				$this->set(array("emplist"=>"emplist"));
				$this->set(array("chkEmp"=> $this->get("chkEmp")));
				/* unset($_SESSION['reportjobs']);
				unset($_SESSION['reportcv']);
				unset($_SESSION['reportjobp']); */
			}else if( $this->get("chkJobS")==2 ){
				$this->set(array("list"=>$employer->getJobSeekerReport($this->get("page"),15,$start_date,$end_date)));
				$this->set(array("joblist"=>"joblist"));
				$this->set(array("chkJobS"=> $this->get("chkJobS")));
				unset($_SESSION['countcv']);
				/* unset($_SESSION['reportemp']);
				unset($_SESSION['reportcv']);
				unset($_SESSION['reportjobp']); */
			}else if( $this->get("chkCV")==3 ){
				$this->set(array("list"=>$employer->getCVReport($this->get("page"),15,$start_date,$end_date)));
				$this->set(array("list1"=>$employer->getCVReport1($this->get("page"),15,$start_date,$end_date))); 
				$this->set(array("cvlist"=>"cvlist"));
				$this->set(array("chkCV"=> $this->get("chkCV")));
				/* unset($_SESSION['reportemp']);
				unset($_SESSION['reportjobs']);
				unset($_SESSION['reportjobp']); */
			}else if( $this->get("chkJobP")==4 ){
				$this->set(array("list"=>$employer->getJobPReport($this->get("page"),15,$start_date,$end_date)));
				$this->set(array("jobplist"=>"jobplist"));
				$this->set(array("chkJobP"=> $this->get("chkJobP")));
				unset($_SESSION['countcv']);
				/* unset($_SESSION['reportemp']);
				unset($_SESSION['reportjobs']);
				unset($_SESSION['reportcv']); */
			}else{
				//vitou display employer today
				$this->set(array("empTodaydisplay"=>$employer->getEmployerReportdisplay($this->get("page"),15,$start_date,$end_date)));
				$this->set(array("empcounttoday"=>"empcounttoday")); 
				unset($_SESSION['countcv']);
				/* unset($_SESSION['reportemp']);
				unset($_SESSION['reportjobs']);
				unset($_SESSION['reportcv']);
				unset($_SESSION['reportjobp']); */
			}
			$empToday=$employer->getEmployerReportToday() ;
			$candToday=$employer->getJobSeekerReportToday() ;
			$cvAttToday=$employer->getResumeAttachmentReportToday() ;
			$cvNoAttToday=$employer->getResumeNoAttachmentReportToday() ;
			$jobStandardToday=$employer->getJobStandardReportToday() ;
			$jobPremiumToday=$employer->getJobPremiumReportToday() ;
			$this->set(array("empToday"=>$empToday));
			$this->set(array("candToday"=>$candToday));
			$this->set(array("cvAttToday"=>$cvAttToday));
			$this->set(array("cvNoAttToday"=>$cvNoAttToday));
			$this->set(array("jobStandardToday"=>$jobStandardToday));
			$this->set(array("jobPremiumToday"=>$jobPremiumToday));
			//set job information
			$job = $this->loadModel("Job");
			$this->set(array("standard_internal"=>$job->get_standard_internal()));
			$this->set(array("standard_external"=>$job->get_standard_external()));
			$this->set(array("premium_internal"=>$job->get_premium_internal()));
			$this->set(array("premium_external"=>$job->get_premium_external()));
			
			$this->render("report");
		}
		public function payment(){
			$this->set(array("current_menu"=>"payment"));
			$this->render("payment");
		}
		public function logout(){
			Session::set("admin",0);
			$this->refresh(array("admin"=>"index"),array(),0);
		}
		public function login(){
			$usrname = $this->get("admin_usr");
			$usrpass = $this->get("admin_pass");
			$result = array("error"=>"1","message"=>"");
			if($usrname!==NULL&&$usrpass!==NULL){
				if($usrname=="admin"&&$usrpass=="1234567890"){
					$result["error"]=0;
					$result["message"]="Successfully login !!!";
					Session::set("admin",1);
				}
				else{
					$result["error"]=1;
					$result["message"]="Failed to login !!!. <br/>Please double check your password or username again !";
				}
			}
			else{
				$result["error"]=1;
				$result["message"]="Username or Password can not be empty";
			}
			echo "(".json_encode($result).")";
		}
		
		/*
			author Mr. Phireak
		*/
		
		public function config(){
			$config = $this->loadModel("Config");
			$this->set(array("config"=>$config->findAsObject(array("id="=>1))));
			$this->render("config");
		}
		/*
		public function setConfig(){
			$config = $this->loadModel("Config");
			$config->save(array(
				"fbshare"=>$this->get("share_point"),
				"fblike"=>$this->get("like_point"),
				"fblikepage"=>$this->get("likepage_point"),
				"introduce_friend" => $this->get("intro_point"),
				"completed_cv" => $this->get("completed_point"),
				"date"=>date("Y-m-d H:i:s")
			));
		}*/
		public function updateConfig(){
			$config = $this->loadModel("Config");
			$this->set(array("configupdate"=>$config->update(array(
				"fbshare"=>$this->get("fbshare"),
				"fblike"=>$this->get("fblike"),
				"fblikepage"=>$this->get("fblikepage"),
				"introduce_friend" => $this->get("intro_friend"),
				"completed_cv" => $this->get("completed_cv"),
				"standard_post" => $this->get("standard_post"),
				"premium_post" => $this->get("premium_post"),
				"buy_resume" => $this->get("buy_resume"),
				"per_premium_post"=> $this->get("per_premium_post"),
				"publish_job"=>$this->get("publish_job") ,
				"duration" => $this->get("duration"),
				"limit_view_standard" => $this->get("limit_view_standard"),
				"limit_view_premium" => $this->get("limit_view_premium"),
				"date"=>date("Y-m-d")),array("id="=>1))));
		}
		//send mail to candidate ,employer,specific user
		public function SendMail(){
			$group= $this->get("group");
			$email_add = $this->get("email");
			$email_group=$this->loadModel("EmailGroup");
			$employer=$this->loadModel("Employer");
			$getemail = $email_group->getEmail($group);
			//$recordEmp = $employer->findObject(array("id = "=>Session::get("employer_id")));
			$str = "Dear" ." Mr. " ." / "." Madame " ."<br/>";
			$str .= "This is a better news letter!";
			//ob_start();
			//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
			Import::util('class.phpmailer');
			//the true param means it will throw exceptions on errors, which we need to catch
			$mail = new PHPMailer(true);
			//telling the class to use SMTP
			$mail->IsSMTP();
			try {
				// SMTP server
				$mail->Host = "mail.atmos-techno.com";
				// enables SMTP debug information (for testing),  show debug : SMTPDebug  = 2,  Hide debug : SMTPDebug  = 1
				$mail->SMTPDebug = 1;
				// enable SMTP authentication
				$mail->SMTPAuth = true;
				// sets the SMTP server
				$mail->Host = "mail.atmos-techno.com";
				// set the SMTP port for the MY OWN DOMAIN server
				$mail->Port = 26;
				// SMTP account username
				$mail->Username = "demo@atmos-techno.com";
				// SMTP account password
				$mail->Password = "demo*123456";
				//$mail->AddAddress('info@atmos-techno.com', 'Atmosphere Technology Co.,Ltd');
				//$mail->AddAddress('phanchamnan15@gmail.com', '');
				if($group != 'individual'){//send mail to cadidates group
					foreach($getemail as $getemail){
						//$mail->AddAddress('phonpisey23@gmail.com','');
						$email_address = filter_input(INPUT_GET, $getemail->email, FILTER_VALIDATE_EMAIL);
						if (!$email_address) {
							$mail->SetFrom('demo@atmos-techno.com', 'HRCambodia');
							
									
									// $mail->AddAddress('info@atmos-techno.com', 'Atmosphere Technology Co.,Ltd');
									$mail->AddAddress($getemail->email, ' ');
								
									//$mail->AddAddress('phonpisey23@gmail.com','');
									$mail->AddReplyTo('demo@atmos-techno.com', 'sales@hrcambodia.com');
									$mail->Subject = "News letter";
									// optional - MsgHTML will create an alternate automatically
									$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
									$mail->MsgHTML("<h3>Hello this is news letter!</h3>" . "<br/>");
									$mail->Send();
									$mail->ClearAddresses();
								
								
								
						 } else {
							//echo 'Email is invalid...';
							break;
							}
						}
						$this->redirect(array("admin"=>"mail"));
					}
					if($group == 'individual'){//send mail to specific user
						//$mail->AddAddress('phonpisey23@gmail.com','');
						$email_address = filter_input(INPUT_GET, $email_add, FILTER_VALIDATE_EMAIL);
						if (!$email_address) {
							$mail->SetFrom('demo@atmos-techno.com', 'HRCambodia');
							$mail->AddAddress($email_add, ' ');
							
							//$mail->AddAddress('phonpisey23@gmail.com','');
							$mail->AddReplyTo('demo@atmos-techno.com', 'sales@hrcambodia.com');
							$mail->Subject = "News letter";
							// optional - MsgHTML will create an alternate automatically
							$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
							$mail->MsgHTML("<h3>Hello this is news letter!</h3>" .  "<br/>");
							$mail->Send();
						} else {
							//echo 'Email is invalid...';
							break;
						}
						$this->redirect(array("admin"=>"mail"));
					}
					
				
			} catch (phpmailerException $e) {
				//Pretty error messages from PHPMailer
				echo $e->errorMessage();
			} catch (Exception $e) {
				//Boring error messages from anything else!
				echo $e->getMessage();
			}
		}
		
		public function addMailGroup(){
			$Email_group = $this->loadModel("EmailGroup");
			$group = $this->get("group");
			$email = $this->get("email");
			$Fname = $this->get("Fname");
			$Lname = $this->get("Lname");
			
				$Email_group->save(array(
						'groups'=>$group,
						"firstname"=>$Fname,
						"lastname"=>$Lname,
						"email"=>$email,
						"created"=>Date("Y-m-d")
						
						));
			
		}
		
		//send mail as news letter
		public function SendMailLeter(){
			$group= $this->get("group");
			$email_add = $this->get("email");
			$email_group=$this->loadModel("EmailGroup");
			$employer=$this->loadModel("Employer");
			//$candidate_email = $email_group->getCandidateEmail($group);
			$getemail = $email_group->getEmail($group);
			//$recordEmp = $employer->findObject(array("id = "=>Session::get("employer_id")));
			//$str = "Dear" .$recordEmp->firstname ." ".$recordEmp->lastname ."<br/>";
			//$str .= "Your job have approved";
			//ob_start();
			//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
			Import::util('class.phpmailer');
			//the true param means it will throw exceptions on errors, which we need to catch
			$mail = new PHPMailer(true);
			//telling the class to use SMTP
			$mail->IsSMTP();
			try {
				// SMTP server
				$mail->Host = "mail.atmos-techno.com";
				// enables SMTP debug information (for testing),  show debug : SMTPDebug  = 2,  Hide debug : SMTPDebug  = 1
				$mail->SMTPDebug = 1;
				// enable SMTP authentication
				$mail->SMTPAuth = true;
				// sets the SMTP server
				$mail->Host = "mail.atmos-techno.com";
				// set the SMTP port for the MY OWN DOMAIN server
				$mail->Port = 26;
				// SMTP account username
				$mail->Username = "demo@atmos-techno.com";
				// SMTP account password
				$mail->Password = "demo*123456";
				//$mail->AddAddress('info@atmos-techno.com', 'Atmosphere Technology Co.,Ltd');
				//$mail->AddAddress('phanchamnan15@gmail.com', '');
				if($group != 'individual'){//send mail to cadidates group
					foreach($getemail as $getemail){
						//$mail->AddAddress('phonpisey23@gmail.com','');
						$email_address = filter_input(INPUT_GET, $getemail->email, FILTER_VALIDATE_EMAIL);
						if (!$email_address) {
							$mail->SetFrom('demo@atmos-techno.com', 'HRCambodia');
								
								
							// $mail->AddAddress('info@atmos-techno.com', 'Atmosphere Technology Co.,Ltd');
							$mail->AddAddress($getemail->email, ' ');
		
							//$mail->AddAddress('phonpisey23@gmail.com','');
							$mail->AddReplyTo('demo@atmos-techno.com', 'sales@hrcambodia.com');
							$mail->Subject = "News letter";
							// optional - MsgHTML will create an alternate automatically
							$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
							$mail->MsgHTML($this->get("message") . "<br/>");
							$mail->Send();
							$mail->ClearAddresses();
		
		
						} else {
							//echo 'Email is invalid...';
							break;
						}
					}
					$this->redirect(array("admin"=>"letter"));
				}
				if($group == 'individual'){//send mail to specific user
					//$mail->AddAddress('phonpisey23@gmail.com','');
					$email_address = filter_input(INPUT_GET, $email_add, FILTER_VALIDATE_EMAIL);
					if (!$email_address) {
						$mail->SetFrom('demo@atmos-techno.com', 'HRCambodia');
						$mail->AddAddress($email_add, ' ');
							
						//$mail->AddAddress('phonpisey23@gmail.com','');
						$mail->AddReplyTo('demo@atmos-techno.com', 'sales@hrcambodia.com');
						$mail->Subject = "News letter";
						// optional - MsgHTML will create an alternate automatically
						$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
						$mail->MsgHTML($this->get("message") .  "<br/>");
						$mail->Send();
					} else {
						//echo 'Email is invalid...';
						break;
					}
					$this->redirect(array("admin"=>"letter"));
				}
				
		
			} catch (phpmailerException $e) {
				//Pretty error messages from PHPMailer
				echo $e->errorMessage();
			} catch (Exception $e) {
				//Boring error messages from anything else!
				echo $e->getMessage();
			}
		}
		public function sendExpireJobToEmployee(){
			$job = $this->loadModel("Job");
			/* staring of job has been expired  */
			$current_date = date('Y-m-d H:i:s');
			$cur_date=strtotime($current_date);
			$deadline=$job->getExpiredToEmployee();
			ob_start();
			//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
			Import::util('class.phpmailer');
			//the true param means it will throw exceptions on errors, which we need to catch
			$mail = new PHPMailer(true);
			//telling the class to use SMTP
			$mail->IsSMTP();
			try {
				// SMTP server
				$mail->Host = "mail.atmos-techno.com";
				// enables SMTP debug information (for testing),  show debug : SMTPDebug  = 2,  Hide debug : SMTPDebug  = 1
				$mail->SMTPDebug = 1;
				// enable SMTP authentication
				$mail->SMTPAuth = true;
				// sets the SMTP server
				$mail->Host = "mail.atmos-techno.com";
				// set the SMTP port for the MY OWN DOMAIN server
				$mail->Port = 26;
				// SMTP account username
				$mail->Username = "demo@atmos-techno.com";
				// SMTP account password
				$mail->Password = "demo*123456";
				//$mail->AddAddress('info@atmos-techno.com', 'Atmosphere Technology Co.,Ltd');
				//$mail->AddAddress('physopheak10@gmail.com', '');
				//$mail->AddAddress('phonpisey23@gmail.com','');
				foreach($deadline as $exp){
					$email_address = filter_input(INPUT_GET, $exp->eemail, FILTER_VALIDATE_EMAIL);
				}
				if (!$email_address) {
					foreach($deadline as $exp){
						$exp_date=strtotime($exp->expire_date);
						if($cur_date > $exp_date){
							$job->updateExpireJob();
							$str = "<b>Your HRCambodia .com Daily Job Alert</b><br/>";
							$str .="<br/><span style='font-family: Trebuchet MS;padding-right:5px'>Post date: " . date('d-M-Y',strtotime($exp->post_date)) ."</span>";
							$str .="<br/><span style='font-family: Trebuchet MS'>Title: " . $exp->title ."</span>";
							$str .="<br/><span style='font-family: Trebuchet MS;padding-right:5px'>Expire date: " . date('d-M-Y',strtotime($exp->expire_date)) ."</span>";
							$mail->SetFrom('demo@atmos-techno.com', 'HRCambodia');
							$mail->AddAddress( $exp->eemail, ' ');
							$mail->AddReplyTo('demo@atmos-techno.com', 'sales@hrcambodia.com');
							$mail->Subject = "Job has been expired !";
							// optional - MsgHTML will create an alternate automatically
							$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
							$mail->MsgHTML($str . "<br/>");
							$mail->Send();
							$mail->ClearAddresses();
						}
					}
				} else {
					echo 'Email is invalid...';
					break;
				}
					
					
			} catch (phpmailerException $e) {
				//Pretty error messages from PHPMailer
				echo $e->errorMessage();
			} catch (Exception $e) {
				//Boring error messages from anything else!
				echo $e->getMessage();
			}
		
			/* ending of job has been expired  */
		}
	}
